class ApplicationController < ActionController::API
  # disable the CSRF token
  # protect_from_forgery with: :null_session
  #
  # # disable cookies (no set-cookies header in response)
  # before_action :destroy_session
  #
  # # disable the CSRF token
  # skip_before_action :verify_authenticity_token
  #
  # #
  # def protect_from_forgery
  #
  # end
  #
  # def verify_authenticity_token
  #
  # end

  def destroy_session
    request.session_options[:skip] = true
  end

  def api_success(opts = {})
    @data = {
      code: !opts[:code].nil? ? opts[:code] : 0,
      msg: !opts[:msg].nil? ? opts[:msg] : '操作成功',
      data: !opts[:data].nil? ? opts[:data] : {},
    }
    # render :inline => @data
    render json: @data, status: "200"
  end

  def api_error(opts = {})
    @status = !opts[:status].nil? ? opts[:status] : 400

    @data = {
      code: !opts[:code].nil? ? opts[:code] : 40000,
      msg: !opts[:msg].nil? ? opts[:msg] : '操作失败',
    }

    # 2种renderjson的写法
    render :json => @data, :status => opts[:status]
    # render json: @data, status: "400"
  end
end
